<template>
    <div class="createPost-container">
      <el-form
      ref="postForm"
      :model="postForm"
      class="form-container"
      :disabled="isView"
    >
      <div class="createPost-main-container">
      <el-collapse v-model="activeNames">
        <el-collapse-item name="1">
          <template slot="title">
            <i class="header-icon el-icon-info"></i>基本信息
          </template>
          <el-row gutter="80">
            <div class="postInfo-container">
              <el-col :span="12">
                <el-row>
                  <el-form-item
                    prop="projectId"
                    label-width="180px"
                    label="项目编号："
                  >
                    <el-input
                      v-model="postForm.projectId"
                      :maxlength="100"
                      name="name"
                      disabled
                    >
                    </el-input>
                  </el-form-item>
                </el-row>
                <el-row>
                  <el-form-item
                    prop="projectName"
                    label-width="180px"
                    label="项目名称："
                  >
                    <el-input
                      v-model="postForm.projectName"
                      :maxlength="100"
                      name="name"
                      disabled
                    >
                    </el-input>
                  </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item
                      label-width="180px"
                      label="项目所属部门:"
                      prop="projectDepartment"
                    >
                      <el-input
                        v-model="postForm.projectDepartment"
                        :maxlength="100"
                        name="name"
                        disabled
                      >
                      </el-input>
                    </el-form-item>
                </el-row>
                <el-row>
                    <el-form-item
                      prop="projectChangeRequestorName"
                      label-width="180px"
                      label="申请人："
                    >
                      <el-input
                        v-model="postForm.projectChangeRequestorName"
                        :maxlength="100"
                        name="name"
                        disabled
                      >
                      </el-input>
                    </el-form-item>
                </el-row>
              </el-col>

              <el-col :span="12">
                <el-row>
                  <el-form-item
                    prop="projectOwnerId"
                    label-width="180px"
                    label="负责人："
                  >
                    <el-input
                      v-model="postForm.projectOwnerName"
                      :maxlength="100"
                      name="name"
                      disabled
                    >
                    </el-input>
                  </el-form-item>
                </el-row>
                <el-row>
                  <el-form-item
                    prop="projectOwnerPhone"
                    label-width="180px"
                    label="负责人电话："
                  >
                    <el-input
                      v-model="postForm.projectOwnerPhone"
                      :maxlength="100"
                      name="name"
                      disabled
                    >
                    </el-input>
                  </el-form-item>
                </el-row>
                <el-row>
                  <el-form-item
                    prop="projectOwnerEmail"
                    label-width="180px"
                    label="负责人邮箱："
                  >
                    <el-input
                      v-model="postForm.projectOwnerEmail"
                      :maxlength="100"
                      name="name"
                      disabled
                    >
                    </el-input>
                  </el-form-item>
                </el-row>
                <el-row>
                  <el-form-item
                    prop="projectChangeType"
                    label-width="180px"
                    label="申请变更类型："
                  >
                    <el-select
                    v-model="postForm.projectChangeType"
                    :remote-method="getTypeList"
                    filterable
                    remote
                    placeholder="请选择变更类型"
                    :loading="loading"
                  >
                    <el-option
                      v-for="item in changeTypeOptions"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    >
                    </el-option>
                  </el-select>
                  </el-form-item>
                </el-row>
              </el-col>
            </div>
          </el-row>
        </el-collapse-item>
        <el-collapse-item name="1" v-if="postForm.projectChangeType === '经费变更' && isView">
            <template slot="title">
              <i class="header-icon el-icon-info"></i>经费信息
            </template>
            <el-row>
              <el-form-item
                label-width="230px"
                label="项目申请经费分项："
                class="postInfo-container-item"
                prop="projectAppliedCostItems"
              >
                <el-table
                  class="appropriation-table"
                  :data="postForm.projectAppliedCostItems"
                  border
                >
                  <el-table-column label="经费预算科目" width="300px" align="center">
                    <template slot-scope="{ row }">
                      <span>{{ row.id}}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="预算标准（万元）" align="center">
                    <template slot-scope="{ row }">
                      <span>{{ row.appropriation}}</span>
                    </template>
                  </el-table-column>
<!--                  <el-table-column label="起始日期" align="center">-->
<!--                    <template slot-scope="{ row }">-->
<!--                      <span>{{ row.startDate}}</span>-->
<!--                    </template>-->
<!--                  </el-table-column>-->
<!--                  <el-table-column label="结束日期" align="center">-->
<!--                    <template slot-scope="{ row }">-->
<!--                      <span>{{ row.endDate}}</span>-->
<!--                    </template>-->
<!--                  </el-table-column>-->
                </el-table>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item
                style="margin-bottom: 40px"
                prop="projectAppliedCost"
                label-width="230px"
                label="项目申请经费总额（万元）："
              >
                <el-input
                  v-model="postForm.projectAppliedCost"
                  :maxlength="100"
                  name="name"
                  disabled
                >
                </el-input>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item
                style="margin-bottom: 40px"
                prop="projectApprovedCost"
                label-width="230px"
                label="项目批准经费："
              >
                <el-input
                  v-model="postForm.projectApprovedCost"
                  :maxlength="100"
                  name="name"
                  disabled
                >
                </el-input>
              </el-form-item>
            </el-row>
        </el-collapse-item>
        <el-collapse-item name="1" v-if="postForm.projectChangeType === '经费变更' && isCreate">
          <template slot="title">
              <i class="header-icon el-icon-info"></i>经费信息
          </template>
          <el-row>
            <el-form-item
              label-width="230px"
              label="项目申请经费分项："
              class="postInfo-container-item"
              prop="projectAppliedCostItems"
            >
              <el-table
                class="appropriation-table"
                :data="postForm.projectAppliedCostItems"
                border
              >
                <el-table-column label="经费预算科目" width="300px" align="center">
                  <template slot-scope="{ row }">
                    <el-select
                      v-model="row.id"
                      filterable
                      remote
                      placeholder="请选择预算科目"
                      :loading="loading"
                      @change="changeAppropriation(row)"
                    >
                      <el-option
                        v-for="item in appropriationListOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column label="预算标准（万元）" align="center">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.appropriation"
                      :maxlength="100"
                      name="name"
                      :disabled="true"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="起始日期" align="center">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.startDate"
                      :maxlength="100"
                      name="name"
                      disabled
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="结束日期" align="center">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.endDate"
                      :maxlength="100"
                      name="name"
                      disabled
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column width="200px" align="center">
                  <template slot-scope="scope">
                    <div class="block float-right">
                      <el-button
                        v-loading="loading"
                        round
                        @click="removeCostItem(scope.row.index)"
                        icon="delete"
                      >
                        删除
                      </el-button>
                      <el-button
                        v-loading="loading"
                        round
                        v-if="
                          scope.$index + 1 ===
                          postForm.projectAppliedCostItems.length
                        "
                        @click="addCostItem(scope.$index)"
                      >
                        添加
                      </el-button>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item
              style="margin-bottom: 40px"
              prop="projectAppliedCost"
              label-width="230px"
              label="项目申请经费总额（万元）："
            >
              <el-input
                v-model="postForm.projectAppliedCost"
                :maxlength="100"
                name="name"
                disabled
              >
              </el-input>
            </el-form-item>
          </el-row>
        </el-collapse-item>
        <el-collapse-item name="1" v-if="postForm.projectChangeType === '成员变更' && isView">
            <template slot="title">
              <i class="header-icon el-icon-info"></i>成员信息
            </template>
            <el-row>
              <el-form-item
                label-width="230px"
                class="postInfo-container-item"
                prop="projectInformationCheckHistory"
              >
                <el-table
                  class="appropriation-table"
                  :data="postForm.projectMembers"
                  border
                >
                  <el-table-column label="队员身份" width="300px" align="center">
                    <template slot-scope="{ row }">
                      <span>{{ row.userProjectRole}}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="员工编号" align="center">
                    <template slot-scope="{ row }">
                      <span>{{ row.employeeId}}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="姓名" align="center">
                    <template slot-scope="{ row }">
                      <span>{{ row.userName}}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="性别" align="center">
                    <template slot-scope="{ row }">
                      <span>{{ row.userGender}}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="电话" align="center">
                    <template slot-scope="{ row }">
                      <span>{{ row.userCellphone}}</span>
                    </template>
                  </el-table-column>
                </el-table>
              </el-form-item>
            </el-row>
        </el-collapse-item>
        <el-collapse-item name="1" v-if="postForm.projectChangeType === '成员变更' && isCreate">
          <template slot="title">
              <i class="header-icon el-icon-info"></i>成员信息
            </template>
          <el-row gutter="80">
          <el-form-item
              label-width="230px"
              label="课题组成员："
              class="postInfo-container-item"
              prop="researchGroupMembers"
            >
              <el-table
                class="members-table"
                :data="postForm.projectMembers"
                border
              >
                <el-table-column label="队员身份" width="180px" align="center">
                  <template slot-scope="{ row }">
                    <el-select
                      v-model="row.userProjectRole"
                      filterable
                      remote
                      placeholder="请选择队员身份"
                      :loading="loading"
                      :remote-method="getMemberRoleList"
                      required
                    >
                      <el-option
                        v-for="item in memberRoleListOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      >
                      </el-option>
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column label="选择队员" width="100px" align="center">
                  <template slot-scope="scope">
                    <el-button round icon="el-icon-user-solid"
                      @click="selectUser(scope.$index)"
                    >
                    </el-button>
                  </template>
                </el-table-column>
                <el-table-column label="员工号" align="center" width="150px">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.employeeId"
                      :maxlength="100"
                      name="name"
                      :disabled="true"
                      required
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="姓名" align="center">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.userName"
                      :maxlength="100"
                      name="name"
                      :disabled="true"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="性别" align="center" width="100px">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.userGender"
                      :maxlength="100"
                      name="name"
                      :disabled="true"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="电话号码" align="center">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.userCellphone"
                      :maxlength="100"
                      name="name"
                      :disabled="true"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column label="身份证号码" align="center">
                  <template slot-scope="{ row }">
                    <el-input
                      v-model="row.userId"
                      :maxlength="100"
                      name="name"
                      :disabled="true"
                    >
                    </el-input>
                  </template>
                </el-table-column>
                <el-table-column width="200px" align="center">
                  <template slot-scope="scope">
                    <div class="block float-right">
                      <el-button
                        v-loading="loading"
                        round
                        @click="removeItem(scope.row.id)"
                        icon="delete"
                      >
                        删除
                      </el-button>
                      <el-button
                        v-loading="loading"
                        round
                        v-if="
                          scope.$index + 1 ===
                          postForm.projectMembers.length
                        "
                        @click="addItem(scope.$index)"
                      >
                        添加
                      </el-button>
                    </div>
                  </template>
                </el-table-column>
              </el-table>
            </el-form-item>
        </el-row>
        </el-collapse-item>
        <el-collapse-item name="1" v-if="postForm.projectChangeType === '延期变更'">
            <template slot="title">
              <i class="header-icon el-icon-info"></i>时间信息
            </template>
            <el-row gutter="80">
              <el-col :span="12">
                <div class="postInfo-container">
            <el-row>
              <el-form-item
                style="margin-bottom: 40px"
                prop="projectCreatedDate"
                label-width="180px"
                label="项目立项日期："
              >
                <el-date-picker
                  v-model="postForm.projectCreatedDate"
                  type="date"
                  placeholder="立项日期"
                  disabled
                >
                </el-date-picker>
              </el-form-item>
            </el-row>
            <el-row>
              <el-form-item
                style="margin-bottom: 40px"
                prop="projectStartDate"
                label-width="180px"
                label="项目开始日期："
              >
                <el-date-picker
                  v-model="postForm.projectStartDate"
                  type="date"
                  placeholder="开始日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-row>
                </div>
              </el-col>
              <el-col :span="12">
                <el-row>
              <el-form-item
                style="margin-bottom: 40px"
                prop="projectExpectedEndDate"
                label-width="180px"
                label="项目计划结项日期："
              >
                <el-date-picker
                  v-model="postForm.projectExpectedEndDate"
                  type="date"
                  placeholder="计划结项日期"
                >
                </el-date-picker>
              </el-form-item>
                </el-row>
                <el-row>
              <el-form-item
                style="margin-bottom: 40px"
                prop="projectEndDate"
                label-width="180px"
                label="项目实际结项日期："
              >
                <el-date-picker
                  v-model="postForm.projectEndDate"
                  type="date"
                  placeholder="实际结项日期"
                  disabled
                >
                </el-date-picker>
              </el-form-item>
                </el-row>
              </el-col>
            </el-row>
        </el-collapse-item>
      </el-collapse>
    </div>
    <el-dialog title="人员信息" class="user-dialog-container" :visible.sync="dialogTableVisible">
        <div class="filter-container">
          <el-input
            v-model="listQuery.searchKey1"
            placeholder="员工号"
            style="width: 200px"
            class="filter-item"
          />
          <el-input
            v-model="listQuery.searchKey2"
            placeholder="姓名"
            style="width: 200px"
            class="filter-item"
          />
          <el-button
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="searchAndSort"
          >
            检索
          </el-button>
        </div>
        <el-table
          :key="tableKey"
          v-loading="listLoading"
          :data="list"
          border
          fit
          highlight-current-row
          style="width: 100%"
        >
          <el-table-column label="" min-width="20px" align="center">
            <template slot-scope="{ row }">
              <el-radio :label="row.id"
                v-model="radio"
                @change.native="singleSelection(row)">&nbsp;</el-radio>
            </template>
          </el-table-column>
          <el-table-column label="员工号" min-width="50px" align="center">
            <template slot-scope="{ row }">
              <span class="link-type">{{ row.employeeId }}</span>
            </template>
          </el-table-column>
          <el-table-column label="名字" min-width="50px" align="center">
            <template slot-scope="{ row }">
              <span class="link-type">{{ row.userName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="性别" min-width="20px" align="center">
            <template slot-scope="{ row }">
              <span class="link-type">{{ row.userGender }}</span>
            </template>
          </el-table-column>
          <el-table-column label="电话号码" min-width="50px" align="center">
            <template slot-scope="{ row }">
              <span class="link-type">{{ row.userCellphone }}</span>
            </template>
          </el-table-column>
          <el-table-column label="身份证号码" min-width="50px" align="center">
            <template slot-scope="{ row }">
              <span class="link-type">{{ row.userId }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
      <el-button
        v-show="!isView"
        v-loading="loading"
        style="margin-left: 230px; width: 100px"
        type="success"
        @click="submitForm"
      >
        保存
      </el-button>
  </el-form>
  </div>
  </template>

  <script>
  import { fetchProjectChangeById, createProjectChange } from "@/api/project-change";
  import { fetchApprovedAppropriationList } from "@/api/appropriation";
  import { fetchProjectById } from "@/api/project";
  import { fetchUserList } from "@/api/user";
  import memberRoles from "@/data/memberRoles";
  import { mapGetters } from "vuex";
  import { searchTwo } from "@/utils";
  import { generateNewDate } from '@/utils/date';
  import projectChangeTypeList from '@/data/projectChangeTypes';
  import { fetchResearchGroupById } from "@/api/research-group";

  const defaultForm = {
    pId: "", // 项目id
    projectId: "", // 项目编号
    projectName: "", // 项目名称
    projectOwnerId: null, // 项目负责人ID
    projectOwnerEmployeeId: "", // 项目负责人编号
    projectOwnerName: "", // 项目负责人名字
    projectOwnerPhone: "", // 项目负责人电话
    projectOwnerEmail: "", // 项目负责人邮箱
    projectDepartment: "", // 项目所属部门
    projectChangeRequestorId: null,
    projectChangeRequestorName: "",
    projectChangeType: "",
    projectMembers: [],
    projectAppliedCost: null,
    projectAppliedCostItems: [],
    projectCreatedDate: null,
    projectStartDate: null,
    projectExpectedEndDate: null,
    projectCheckStatus: "",
    projectChangeRequestDate: null,
    projectReviewComment: "",
    projectReviewerId: null,
    projectReviewerName: "",
    projectReviewerDate: null,
    projectGroupName: "",
    projectGroupId: null,
    projectStatus: "",
    id: undefined,
  };

  export default {
    name: "ProjectDetail",
    props: {
      isView: {
        type: Boolean,
        default: false,
      },
      isCreate: {
        type: Boolean,
        default: false,
      },
    },
    data() {
      return {
        tableKey: 0,
        postForm: Object.assign({}, defaultForm),
        loading: false,
        listLoading: false,
        activeNames: ["1"],
        appropriationList: [],
        appropriationListOptions: [],
        userList: [],
        userListOptions: [],
        changeTypeOptions: projectChangeTypeList,
        memberRoleListOptions: memberRoles,
        currentMemberRowId: 0,
        totalCost: 0,
        list: [],
        totalList: [],
        listQuery: {
        pageIndex: 1,
        limit: 1000,
        searchField1: "employeeId",
        searchField2: "userName",
        searchKey1: "",
        searchKey2: "",
        },
        dialogTableVisible: false,
      };
    },
    computed: {
    ...mapGetters(["id", "name"])
  },
    created() {
      this.getAppropriations();
      this.getUsers();
      if (this.isView) {
        const id = this.$route.params && this.$route.params.id;
        this.fetchData(id);
      } else if (this.isCreate) {
        const projectId = this.$route.params && this.$route.params.id;
        this.fetchProjectData(projectId);
      }
    },
    methods: {
      getTypeList(query) {
      if (query !== "") {
        this.loading = true;
        this.changeTypeOptions = projectChangeTypeList.filter((item) => {
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
        this.loading = false;
      } else {
        this.changeTypeOptions = projectChangeTypeList;
      }
    },
      getAppropriations() {
        this.listLoading = true;
        fetchApprovedAppropriationList().then((res) => {
        this.appropriationList = res.data.map((item) => {
          return {
            label: item.appropriationName,
            value: item.id,
            appropriation: item.appropriationStandard,
            startDate: item.appropriationStart,
            endDate: item.appropriationEnd,
          };
        });
        this.appropriationListOptions = this.appropriationList;

        this.listLoading = false;
      });
    },
    getUsers() {
      this.listLoading = true;
      fetchUserList().then((res) => {
        this.totalList = res.data;
        this.userList = res.data.map((item) => {
          return {
            label: item.userName,
            value: item.id,
            phone: item.userCellphone,
            email: item.userEmail,
          };
        });
        this.userListOptions = this.userList;

        this.listLoading = false;
      });
    },
    getUserList(query) {
      if (query !== "") {
        this.loading = true;
        this.userListOptions = userList.filter((item) => {
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
        this.loading = false;
      } else {
        this.userListOptions = userList;
      }
    },
    getMemberRoleList(query) {
      if (query !== "") {
        this.loading = true;
        this.memberRoleListOptions = memberRoles.filter((item) => {
          return item.label.toLowerCase().indexOf(query.toLowerCase()) > -1;
        });
        this.loading = false;
      } else {
        this.memberRoleListOptions = memberRoles;
      }
    },
    addItem(val) {
      let checkEmptyItems = this.postForm.projectMembers.filter(
        (item) => item.employeeId === null || item.userProjectRole === null
      );
      if (
        checkEmptyItems.length >= 1 &&
        this.postForm.projectMembers.length > 0
      ) {
        this.$message({
          message: "成员所有信息不能为空",
          type: "error",
        });
        return;
      }
      this.postForm.projectMembers.push({
        id: val + 1,
        userProjectRole: null,
        employeeId: null,
        userName: null,
        userGender: null,
        userCellphone: null,
        userId: null,
        researchGroupMemberId: null
      });
    },
    removeItem(itemId) {
      if (this.postForm.projectMembers.length > 1) {
        this.postForm.projectMembers.splice(itemId, 1);
      } else {
        this.postForm.projectMembers = [{
        id: null,
        userProjectRole: null,
        employeeId: null,
        userName: null,
        userGender: null,
        userCellphone: null,
        userId: null,
        researchGroupMemberId: null
       }];
      }
    },
    selectUser(val) {
      this.currentMemberRowId = val;
      this.dialogTableVisible = true;
      this.handleFilter();
    },
    singleSelection(row) {
      this.postForm.projectMembers.forEach((item) => {
        if (item.id === this.currentMemberRowId) {
          item.employeeId = row.employeeId;
          item.userName = row.userName;
          item.userGender = row.userGender;
          item.userCellphone = row.userCellphone;
          item.userId = row.userId;
          item.researchGroupMemberId = row.id;
        };
      });
      this.dialogTableVisible = false;
    },
    handleFilter() {
      const result = searchTwo(this.totalList, this.listQuery);
      this.list = result.data;
      this.total = result.total;
    },
    searchAndSort() {
      this.listQuery.pageIndex = 1;
      this.handleFilter();
    },
    caculateTotalCost() {
      let totalCost = 0;

      this.postForm.projectAppliedCostItems.forEach((item) => {
        totalCost += item.appropriation ? item.appropriation : 0;
      });
      this.postForm.projectAppliedCost = totalCost;
    },
    changeAppropriation(val) {
      const selectedItem = this.appropriationList.find((item) => {
        return item.value === val.id;
      });
      for (const item of this.postForm.projectAppliedCostItems) {
        if (item.id === val.id) {
          item.name = selectedItem.label;
          item.appropriation = selectedItem.appropriation;
          item.startDate = selectedItem.startDate;
          item.endDate = selectedItem.endDate;
          break;
        }
      }
      this.caculateTotalCost();
    },
    addCostItem(val) {
      let checkEmptyItems = this.postForm.projectAppliedCostItems.filter(
        (item) => item.appropriation === null
      );
      if (
        checkEmptyItems.length >= 1 &&
        this.postForm.projectAppliedCostItems.length > 0
      ) {
        this.$message({
          message: "费用信息不能为空",
          type: "error",
        });
        return;
      }
      this.postForm.projectAppliedCostItems.push({
        index: val + 1,
        id: null,
        name: null,
        appropriation: null,
        startDate: null,
        endDate: null,
      });
    },
    removeCostItem(itemIndex) {
      if (this.postForm.projectAppliedCostItems.length > 1) {
        this.postForm.projectAppliedCostItems.splice(itemIndex, 1);
      } else {
        this.postForm.projectAppliedCostItems = [{
        index: 0,
        id: null,
        name: null,
        appropriation: null,
        startDate: null,
        endDate: null,
      }];
      }
      this.caculateTotalCost();
    },
    // changeType(val) {
    //   if (val === "成员变更") {
    //     this.postForm.projectChangeType = "成员变更";
    //   } else if (val === "经费变更") {
    //     this.postForm.projectChangeType = "经费变更";
    //   } else if (val === "延期变更") {
    //     this.postForm.projectChangeType = "延期变更";
    //   }
    // },
    fetchData(id) {
        this.loading = true;
        fetchProjectChangeById(id)
          .then((res) => {
            this.postForm = res.data[0];
            this.postForm.projectAppliedCostItems =
              this.postForm.projectAppliedCostItems?.length > 0
                ? JSON.parse(this.postForm.projectAppliedCostItems)
                : [];

            this.postForm.projectMembers = this.postForm.projectMembers?.length > 0
                ? JSON.parse(this.postForm.projectMembers)
                : [];
            this.loading = false;
          })
          .catch((err) => {
            console.log(err);
            this.loading = false;
          });
      },
      fetchProjectData(id) {
        this.loading = true;
        fetchProjectById(id)
        .then((res) => {
          this.postForm.pId = res.data[0].id;
          this.postForm.projectId = res.data[0].projectId;
          this.postForm.projectName = res.data[0].projectName;
          this.postForm.projectOwnerId = res.data[0].projectOwnerId;
          this.postForm.projectOwnerEmployeeId = res.data[0].projectOwnerEmployeeId;
          this.postForm.projectOwnerName = res.data[0].projectOwnerName;
          this.postForm.projectOwnerPhone = res.data[0].projectOwnerPhone;
          this.postForm.projectOwnerEmail = res.data[0].projectOwnerEmail;
          this.postForm.projectDepartment = res.data[0].projectDepartment;
          this.postForm.projectChangeRequestorId = this.id;
          this.postForm.projectChangeRequestorName = this.name;
          this.postForm.projectCreatedDate = res.data[0].projectCreatedDate;
          this.postForm.projectCheckStatus = "待审批";
          this.postForm.projectChangeRequestDate = generateNewDate();
          this.postForm.projectGroupName = res.data[0].projectGroupName;
          this.postForm.projectGroupId = res.data[0].projectGroupId;
          this.postForm.projectStatus = res.data[0].projectStatus;
          this.postForm.projectAppliedCost = res.data[0].projectAppliedCost;
          this.postForm.projectAppliedCostItems =
          res.data[0].projectAppliedCostItems?.length > 0
              ? JSON.parse(res.data[0].projectAppliedCostItems)
              : [];
          fetchResearchGroupById(res.data[0].projectGroupId)
              .then((groupRes) => {
                this.postForm.projectMembers = groupRes.data[0].researchGroupMembers?.length > 0
                  ? JSON.parse(groupRes.data[0].researchGroupMembers)
                  : [];
                this.loading = false;
              })
              .catch((err) => {
                console.log(err);
                this.loading = false;
              });
          this.loading = false;
        })
        .catch((err) => {
          console.log(err);
          this.loading = false;
        });
      },
      submitForm() {
        this.$refs.postForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if(this.postForm.projectChangeType === "成员变更") {
            this.postForm.projectAppliedCostItems = [];
            this.postForm.projectMembers =
            this.postForm.projectMembers.filter((item) => {
              return item && item.employeeId && item.userProjectRole
            });
          } else if(this.postForm.projectChangeType == "经费变更") {
            this.postForm.projectMembers = [];
            this.postForm.projectAppliedCostItems =
            this.postForm.projectAppliedCostItems.filter((item) => {
              return item && item.id
            });
          }
            createProjectChange(this.postForm)
              .then((res) => {
                if (res.data) {
                  this.$router.push("/project/change/list");

                  this.$message({
                    title: "成功",
                    message: "申请变更创建成功",
                    type: "success",
                    duration: 2000,
                  });
                } else {
                  this.$message({
                    message: "申请变更创建失败",
                    type: "error",
                  });
                }
                this.loading = false;
              })
              .catch((err) => {
                this.loading = false;
              });
        } else {
          console.log("申请变更创建失败");
          return false;
        }
      });
    },
    },
  };
  </script>

  <style lang="scss" scoped>
  @import "~@/styles/mixin.scss";

  .createPost-container {
    position: relative;

    .steps {
      margin: 50px 0 30px 0;
    }

    .file-uploader .el-upload {
      border: 1px dashed #d9d9d9 !important;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .file-uploader .el-upload:hover {
      border-color: #409eff;
    }

    .createPost-main-container {
      padding: 40px 45px 20px 50px;

      .postInfo-container {
        position: relative;
        @include clearfix;
        margin-bottom: 10px;

        .postInfo-container-item {
          float: left;
        }
      }
    }
  }

  .identity-container {
    margin-top: 50px;
  }
  </style>
